import {serialize} from '@/util/util'
import {getStore} from '../util/store'
import NProgress from 'nprogress' // progress bar
import errorCode from '@/const/errorCode'
import router from "@/router/router"
import {Message} from 'element-ui'
import 'nprogress/nprogress.css'
import store from "@/store"; // progress bar style
import {basePath} from "@/config/env.js"
import axios from 'axios'
axios.defaults.timeout = 30000
// 返回其他状态吗
axios.defaults.validateStatus = function (status) {
  return status >= 200 && status <= 500 // 默认的
}
// 跨域请求，允许保存cookie
axios.defaults.withCredentials = true
// NProgress Configuration
NProgress.configure({
  showSpinner: false
})

// HTTPrequest拦截
axios.interceptors.request.use(config => {
  NProgress.start() // start progress bar
  if (config.url && config.url.toLowerCase().indexOf('http:')!=0&&config.url.toLowerCase().indexOf('https')!=0){
    if (basePath.lastIndexOf('/')!=basePath.length-1 && config.url.indexOf('/')!=0)
      config.url = basePath + '/' + config.url;
    else
      config.url = basePath + config.url;
  }
  //const TENANT_ID = getStore({name: 'tenantId'})
  //const isToken = (config.headers || {}).isToken === false
  let token =  store.getters.access_token
  if (token) {
    config.headers['chcitToken'] = token// token
  }
  let authToken =  store.getters.auth_token
  if (authToken) {
    config.headers['Authorization'] = 'Bearer ' + authToken// token
  }
//  if (TENANT_ID) {
//    config.headers['TENANT_ID'] = TENANT_ID // 租户ID
//  }
  // headers中配置serialize为true开启序列化
  if (config.method === 'post' && config.headers.serialize) {
    if(config.headers.contetTypeForm){
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    }
    config.data = serialize(config.data)
    delete config.data.serialize
  }
  return config
}, error => {
  return Promise.reject(error)
})


// HTTPresponse拦截
axios.interceptors.response.use(res => {
  NProgress.done()
  const status = Number(res.status) || 200
  const message = res.data.msg ||("No message available"==res.data.message?undefined:res.data.message) || errorCode[status] || errorCode['default']
  if (status === 401) {
    store.dispatch('FedLogOut').then(() => {
      router.push({path: '/login'})
    })
    return
  }

  if (status !== 200 || (res.data &&res.data.code=== 1)) {
    Message({
      message: message,
      type: 'error'
    })
    return Promise.reject(new Error(message))
  }

  return res
}, error => {
  NProgress.done()
  return Promise.reject(new Error(error))
})

export default axios
